'''
https://leetcode.cn/problems/insertion-sort-list/description/
'''
from typing import Optional


# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        help = []
        cur = head
        while cur:
            help.append(cur)
            cur = cur.next
        help.sort(key=lambda x: x.val)
        for i in range(len(help)-1):
            help[i].next = help[i+1]
        help[-1].next = None
        return help[0]

    def insertionSortList2(self, head: Optional[ListNode]) -> Optional[ListNode]:
        help = []
        cur = head
        while cur:
            help.append(cur)
            cur = cur.next
        help.sort(key=lambda x: x.val)
        for i in range(len(help)-1):
            help[i].next = help[i+1]
        help[-1].next = None
        return help[0]